//
//  AlertViewWithOneBtn.swift
//  GreenDeal
//
//  Created by Moennig Q on 22/2/2023.
//

import UIKit
import RxSwift
import RxCocoa

class AlertViewWithOneBtn: UIView {

    let disposeBag = DisposeBag()
    let titleLabel = ContentLabel(R.font.interBold(size: 17))
    let messageLabel: BaseTextView = {
        let view = BaseTextView()
        view.isEditable = false
        view.textColor = UIColor(hexStr: "#292D32")
        return view
    }()
    //
    var okClosure: (()->Void)?
    
    let okBtn = ViewFactory.shared.generateButtonOnAlertView()
    //IB init
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }
    //纯代码创建
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpView()
    }
    init(title: String = "", message: String, messageTextAlignment: NSTextAlignment = .center, confirmStr: String = "OK", okClosure: (()->Void)? = nil) {
        super.init(frame: CGRect.zero)
        self.titleLabel.text = title
        self.messageLabel.setAttributedString(content: message,font: R.font.interRegular(size: 15))//设置content按照word wrapping
        self.messageLabel.text = message
        self.messageLabel.textAlignment = messageTextAlignment
        self.okBtn.setTitle(confirmStr, for: UIControl.State.normal)
        self.okClosure = okClosure
        self.setUpView()
    }
    //
    private func setUpView() {
        self.backgroundColor = UIColor(hexStr: "#00000080")
        //back view
        var maxWidth: CGFloat = 300
        if UIDevice.getCurrentDeviceType() == APPConfig.iPadType {
            maxWidth = 500
        }
        let backView = UIView()
        backView.drawCorner(byRoundingCorners: .allCorners,radii: 10)
        backView.backgroundColor = UIColor.white
        self.addSubview(backView)
        backView.layer.cornerRadius = Constant.BackViewCornerRadius
        backView.snp.makeConstraints { (make) in
            make.width.equalTo(maxWidth)
            make.height.greaterThanOrEqualTo(0)
            make.center.equalToSuperview()
        }
        //titile
        let leftRightMargin: CGFloat = 20
        let okBtnHeight: CGFloat = 48
        let horizonalLineViewTopMargin: CGFloat = 15
        let titleLabelTopMargin: CGFloat = 10
        titleLabel.numberOfLines = 0
        titleLabel.textAlignment = NSTextAlignment.center
        backView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabelTopMargin)
            make.left.equalTo(leftRightMargin)
            make.right.equalTo(-leftRightMargin)
            make.height.greaterThanOrEqualTo(0)
        }
        
        //message
        let topBottomMargin = UIDevice.getStatusBarHeight() + UIDevice.getBottomAreaHeight() + 60
        let titleContent = self.titleLabel.text ?? ""
        let messageLabelTopMargin: CGFloat = (titleContent == "" ? 0 : 10)
        let titleRealHeight = titleLabel.sizeThatFits(CGSize(width: maxWidth - leftRightMargin * 2, height: CGFloat(MAXFLOAT))).height
        //真正的高度
        let msgRealHeight = messageLabel.sizeThatFits(CGSize(width: maxWidth - leftRightMargin * 2, height: CGFloat(MAXFLOAT))).height
        let maxHeight = APPConfig.HeightForPortrait - topBottomMargin - titleLabelTopMargin - titleRealHeight - titleLabelTopMargin - messageLabelTopMargin - horizonalLineViewTopMargin - okBtnHeight //屏幕可用的最大高度
        if msgRealHeight > maxHeight {
            messageLabel.isScrollEnabled = true
        }else{
            messageLabel.isScrollEnabled = false
        }
        backView.addSubview(messageLabel)
        messageLabel.snp.makeConstraints { (make) in
            if titleContent == ""{//标题为空时，顶部和title的对齐
                make.top.equalTo(titleLabel)
            }else{//标题有值时，
                make.top.equalTo(titleLabel.snp.bottom).offset(messageLabelTopMargin)
            }
            make.left.right.equalTo(titleLabel)
            if msgRealHeight > maxHeight {//内容大于屏幕的最大可用高度
                make.height.equalTo(maxHeight)
            }else{
                make.height.equalTo(msgRealHeight)
            }
        }
        //horizonal line view
        let horizonalLineView = View(hexStr: "#0000001A")
        backView.addSubview(horizonalLineView)
        horizonalLineView.snp.makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp.bottom).offset(horizonalLineViewTopMargin)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        //Yes button
        backView.addSubview(okBtn)
        okBtn.snp.makeConstraints { (make) in
            make.top.equalTo(horizonalLineView)
            make.left.right.equalToSuperview()
            make.height.equalTo(okBtnHeight)
            make.bottom.equalToSuperview()
        }
        okBtn.rx.tap.subscribe { [weak self] _ in
            self?.okClosure?()
            self?.removeFromSuperview()
        }.disposed(by: self.disposeBag)
    }
}
